package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.pojo.*;
import com.itheima.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private ShoppingCartService shoppingCartService;


    @Override
    public void submitOrder(Orders orders) {
        Long userId = BaseContext.getCurrentId();
        //雪花算法生成order.id
        long orderId = IdWorker.getId();
        //查询用户信息
        User user = userService.getById(userId);
        //查询地址信息
        AddressBook addressBook = addressBookService.getById(orders.getAddressBookId());
        //根据用户id查询购物车
        List<ShoppingCart> list = shoppingCartService.lambdaQuery()
            .eq(ShoppingCart::getUserId, userId)
            .list();

        //计算总金额,定义
        BigDecimal total = new BigDecimal("0");
        //2.添加订单详细
        // order_detail，多条数据
        //todo 补全list数据
        ArrayList<OrderDetail> orderDetailList = new ArrayList<>();

        for (ShoppingCart cart : list) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(cart.getNumber());
            orderDetail.setDishFlavor(cart.getDishFlavor());
            orderDetail.setDishId(cart.getDishId());
            orderDetail.setSetmealId(cart.getSetmealId());
            orderDetail.setName(cart.getName());
            orderDetail.setImage(cart.getImage());
            orderDetail.setAmount(cart.getAmount());

            BigDecimal multiply = orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()));
            total = total.add(multiply);
            //todo 往集合中添加orderDetail对象
            orderDetailList.add(orderDetail);
        }
        //1.添加订单
        // order,一条数据
        //todo 补全order的数据
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);//订单状态：1.待付款，2.待派送，3.正在派送，4.已完成，5.已取消
        orders.setUserId(userId);
        orders.setId(orderId);
        orders.setNumber(String.valueOf(orderId));
        orders.setUserName(user.getName());
        orders.setConsignee(addressBook.getConsignee());//收件人
        orders.setPhone(addressBook.getPhone());//收货人手机号
        orders.setAddress(addressBook.getDetail());//收货地址
        orders.setAmount(total);//总金额
        this.save(orders);
        //获取用户id

     //3.清理购物车数据
        shoppingCartService.lambdaUpdate().eq(ShoppingCart::getUserId,userId).remove();


    }
}
